Imports System.Windows.Forms

Public Class frmNewProject

    Private Sub frmNewProject_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            With Project
                lblWorkspaceName.Text = clsApplicationSettings.WorkspaceName
                lblConfigFile.Text = clsProject.ReplaceMyDocs(IO.Path.Combine(clsApplicationSettings.AppDataPath, "WRDB6.ini"))
                lblProjectName.Text = .Name
                lblDescription.Text = .Description
                lblProvider.Text = [Enum].GetName(.DB.DatabaseType.GetType, .DB.DatabaseType).Replace("_", " ")
                lblStations.Text = .DB.GetRecordCount("Stations").ToString("N0")
                lblNumTables.Text = .GetTableList().Count
                Dim NumRecords As Integer = 0
                For Each tbl As String In .GetTableList
                    NumRecords += Math.Max(0, .GetTrackingRecordCount(tbl))
                Next
                NumRecords += .DB.GetRecordCount(.MasterTableName)
                lblNumRecords.Text = NumRecords.ToString("N0")
            End With
            AddProjects()
            AddTables()
            MainWRDB.CloseSplashScreen()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub AddProjects()
        With mnuOpenProject.Items
            .Clear()
            Dim ar() As String = MainForm.mruMenuProj.GetFiles()
            If ar IsNot Nothing Then
                For Each s As String In ar
                    If clsConnections.GetProjectList.ContainsKey(s) Then .Add(s)
                Next
            End If
        End With
    End Sub

    Private Sub AddTables()
        Try
            With mnuOpenTable.Items
                .Clear()
                MainForm.mruMenuTable.LoadFromRegistry(MRUREGISTRY & "\" & Project.Name)
                For Each s As String In MainForm.mruMenuTable.GetFiles
                    If Not String.Equals(s, Project.MasterTableName, StringComparison.CurrentCultureIgnoreCase) Then .Add(s)
                Next
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnOpenProject_Click(sender As Object, e As System.EventArgs) Handles btnOpenProject.Click
        Try
            Enabled = False
            MainForm.FileOpenProject()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        Finally
            Enabled = True
        End Try
    End Sub

    Private Sub mnuOpenProject_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuOpenProject.ItemClicked
        Try
            Enabled = False
            SetProject(e.ClickedItem.Text, , True)
            MainForm.AddMruProject()
            AddProjects()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        Finally
            Enabled = True
        End Try
    End Sub

    Private Sub btnOpenTable_Click(sender As Object, e As System.EventArgs) Handles btnOpenTable.Click
        Try
            Enabled = False
            MainForm.FileOpenWorking()
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        Finally
            Enabled = True
        End Try
    End Sub

    Private Sub mnuOpenTable_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuOpenTable.ItemClicked
        Try
            Enabled = False
            If MainForm.OpenDataTable(e.ClickedItem.Text) Then
                MainForm.AddMruTable()
            Else
                Enabled = True
                MainForm.mruMenuTable.RemoveFile(e.ClickedItem.Text)
                MainForm.mruMenuTable.SaveToRegistry(MRUREGISTRY & "\" & Project.Name)
                AddTables()
                Exit Sub
            End If
            Close()
        Catch ex As Exception
            ErrorMsg(, ex)
        Finally
            Enabled = True
        End Try
    End Sub

    Private Sub btnExplore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExplore.Click
        Try
            If Not SupervisorEnabled() Then Exit Sub

            Close()
            Enabled = False
            Visible = False
            MainForm.FileMgmtExplore()
        Catch ex As Exception
            ErrorMsg(, ex)
        Finally
            Enabled = True
        End Try
    End Sub

    Private Sub btnOtherTasks_Click(sender As Object, e As System.EventArgs) Handles btnOtherTasks.Click
        Close()
        frmInfoMsg.ShowInfoMsg("Other Project Tasks", "You just clicked the Other Project Tasks button. This keeps the just-selected Project active, closes the New Project form, and allows you to select from the many additional available tasks using WRDB's main menu and toolbar.")
    End Sub

    Private Sub btnOpenMaster_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenMaster.Click
        Close()
        MainForm.FileOpenMaster()
    End Sub

    Private Sub btnOpenSupport_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenSupport.Click
        Close()
        MainForm.FileOpenSupport()
    End Sub

    Private Sub btnEditPrefs_Click(sender As System.Object, e As System.EventArgs) Handles btnPrefs.Click
        Try

            If Not SupervisorEnabled() Then Exit Sub
            Close()
            Enabled = False
            Visible = False
            MainForm.FileProjectPrefs()

        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

End Class
